<?php
require(__META_CONTROLS_GEN__ . '/UserMetaControlGen.class.php');

/**
 * This is a MetaControl customizable subclass, providing a QForm or QPanel access to event handlers
 * and QControls to perform the Create, Edit, and Delete functionality of the
 * User class.  This code-generated class extends from
 * the generated MetaControl class, which contains all the basic elements to help a QPanel or QForm
 * display an HTML form that can manipulate a single User object.
 *
 * To take advantage of some (or all) of these control objects, you
 * must create a new QForm or QPanel which instantiates a UserMetaControl
 * class.
 *
 * This file is intended to be modified.  Subsequent code regenerations will NOT modify
 * or overwrite this file.
 *
 * @package My QCubed Application
 * @subpackage MetaControls
 */
class UserMetaControl extends UserMetaControlGen {
    // Initialize fields with default values from database definition
    /*
		public function __construct($objParentObject, User $objUser) {
			parent::__construct($objParentObject,$objUser);
			if ( !$this->blnEditMode ){
				$this->objUser->Initialize();
			}
		}
    */
    protected $txtOldPassword;
    public $blnAdmin=false;
    public $txtPassword1;
    public $txtPassword2;

    public static function CreateFromPathInfo($objParentObject, $intCreateType = QMetaControlCreateType::CreateOrEdit) {
        $intId = QApplication::PathInfo(0);

        return UserMetaControl::Create($objParentObject,  QS_DECODE($intId), $intCreateType);
    }

    public function lstPerson_Create($strControlId = null) {
        //$this->lstPerson = new QAjaxAutoCompleteTextBox($this->objParentObject, 'lstPerson_Change',$strControlId);
        $this->lstPerson = new QAjaxAutoCompleteTextBox($this->objParentObject, 'lstPerson_Change',$strControlId);
        //$this->lstPerson->SetSelectCallback($this, 'txt_Select');

        $this->lstPerson->Name = QApplication::Translate("<a href='". __CONTACTS__ . "/Organization_edit.php' target='_blank'>Person</a>");
        $this->lstPerson->MustMatch=true;
        if($this->objUser->Person) $this->lstPerson->Text = $this->objUser->Person->__toString();
        $this->lstPerson->MaxLength = Person::FirstNameMaxLength + Person::LastNameMaxLength;
        return $this->lstPerson;
    }
    public function txt_Select($mixSelected) {
        $strParamArray = explode('|', $mixSelected);
        if (count($strParamArray) == 2) {
            $this->lstPerson->Value = (int)$strParamArray[1];
        }
    }

    public function lstRolesAsEffective_Create($strControlId = null) {
        $this->lstRolesAsEffective = new AutoCompleteList($this->objParentObject, $strControlId);
        $this->lstRolesAsEffective->Name = QApplication::Translate('Effective Roles');
        $objAssociatedArray = $this->objUser->GetRoleAsEffectiveArray();
        $objRoleArray = Role::LoadAll();
        if ($objRoleArray) foreach ($objRoleArray as $objRole) {
                foreach ($objAssociatedArray as $objAssociated) {
                    if ($objAssociated->Id == $objRole->Id)
                        $this->lstRolesAsEffective->lst->AddItem($objRole->__toString(), $objRole->Id);
                }

            }
        $this->lstRolesAsEffective->strCallList = "Role_Change";
        $this->lstRolesAsEffective->strReverseCall = "Role_Reverse";
        return $this->lstRolesAsEffective;
    }



    public function txtOldPassword_create($strControlId = null) {
        $this->txtOldPassword = new QTextBox($this->objParentObject, $strControlId);
        $this->txtOldPassword->Name = QApplication::Translate("Old Password");
        $this->txtOldPassword->TextMode = QTextMode::Password;
        return $this->txtOldPassword;
    }

    public function txtPassword1_create($strControlId = null) {
        $this->txtPassword1 = new QTextBox($this->objParentObject, $strControlId);
        $this->txtPassword1->Name = QApplication::Translate("Password");
        $this->txtPassword1->TextMode = QTextMode::Password;
        return $this->txtPassword1;
    }

    public function txtPassword2_create($strControlId = null) {
        $this->txtPassword2 = new QTextBox($this->objParentObject, $strControlId);
        $this->txtPassword2->Name = QApplication::Translate("Retype Password");
        $this->txtPassword2->TextMode = QTextMode::Password;
        return $this->txtPassword2;
    }



    protected function lstRolesAsEffective_Update() {
        if ($this->lstRolesAsEffective) {
            $this->objUser->UnassociateAllRolesAsEffective();
            $imax = $this->lstRolesAsEffective->lst->__get("ItemCount");
            for ($i=0; $i <$imax; $i++) {
                $objListItem = $this->lstRolesAsEffective->lst->GetItem($i);
                $this->objUser->AssociateRoleAsEffective(Role::Load($objListItem->Value));
            }
        }
    }

    public function SaveUser() {
        try {

            $UserSys = new Perm();
            if ( strlen( $this->txtPassword1->Text ) > 0 ) {
                if ($this->blnAdmin == true)
                    $this->objUser->Password = $UserSys->md5crypt(  $this->txtPassword1->Text );
                else {
                    $this->objUser->Password = $UserSys->md5crypt(  $this->txtPassword1->Text );
                }
            }
            // Update any fields for controls that have been created
            if ($this->txtUserName) $this->objUser->UserName = $this->txtUserName->Text;

            if ($this->lstPerson) $this->objUser->PersonId = $this->lstPerson->SelectedValue();


            if ($this->lstUserTypeObject) $this->objUser->UserType = $this->lstUserTypeObject->SelectedValue;

            if ($this->chkActive) $this->objUser->Active = $this->chkActive->Checked;

            if ($this->lstPrimaryRole) $this->objUser->PrimaryRoleId = $this->lstPrimaryRole->SelectedValue;


            // Save the User object
            $this->objUser->Save();

            // Finally, update any ManyToManyReferences (if any)
            $this->lstProjectsAsMembers_Update();
            $this->lstRolesAsEffective_Update();
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }

}
?>